package edu.guigu.appquickstart.adapterview.subitem;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.adapterview.bean.UserBean;

public class GridviewActivity extends AppCompatActivity {

    private GridView gridView;
    private MyBaseAdapter adapter;
    private ArrayList<UserBean> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        // Actionbar
        setSupportActionBar(findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // 1 准备数据 以及 布局
        datas = new ArrayList<UserBean>();
        for (int i = 0; i < 50; i++) {
            final String url = "https://avatars.githubusercontent.com/u/" + (i + 1) + "?v=4";
            UserBean userBean = new UserBean("张三" + (i + 1), url);
            datas.add(userBean);
        }
        // 2 准备适配器
        adapter = new MyBaseAdapter(this);
        // 3 数据交给适配器
        adapter.setDataList(datas);
        // 4 gridview设置adapter
        gridView = findViewById(R.id.gridview);
        gridView.setAdapter(adapter);

        // 列表监听 (弹出对话框实现删除)
        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                delDialogBox(position);
                return false;
            }
        });
    }

    private void delDialogBox(int position) {
        final UserBean userBean = datas.get(position);
        final AlertDialog alertDialog = new AlertDialog.Builder(GridviewActivity.this)
                .setTitle("警告")
                .setMessage("确认删除 "+userBean.getLogin()+" 吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    // 删除数据
                    datas.remove(position);
                    // 适配器刷新
                    adapter.notifyDataSetChanged();
                })
                .setNegativeButton("取消", null)
                .setCancelable(false)
                .create();
        alertDialog.show();
    }

    // 设置菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_gridview, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // 设置菜单
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        // 菜单add的事件处理
        if (item.getItemId() == R.id.menu_add) {
            final String name = "玩家"+Math.random()*100;
            final String str = "http://abc/a.jpg";
            // 添加数据
            UserBean userBean = new UserBean(name,str);
            datas.add(userBean);
            // 适配器刷新
            adapter.notifyDataSetChanged();
            // 滑动到最后一项位置
            gridView.smoothScrollToPosition(datas.size()-1);
        }
        return super.onOptionsItemSelected(item);
    }
}

class MyBaseAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<UserBean> dataList = new ArrayList<>();

    public ArrayList<UserBean> getDataList() {
        return dataList;
    }

    public void setDataList(ArrayList<UserBean> dataList) {
        this.dataList = dataList;
    }

    public MyBaseAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public UserBean getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolde viewHolde = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_grid_, parent, false);
            viewHolde = new ViewHolde(convertView);
            convertView.setTag(viewHolde);
        } else {
            viewHolde = (ViewHolde) convertView.getTag();
        }

        // 更新视图数据
        UserBean userBean = getItem(position);
        viewHolde.textView.setText(userBean.getLogin());
        Glide.with(context)
                .load(userBean.getAvatar_url())
                .placeholder(R.drawable.ic_loading)
                .error(R.drawable.ic_image)
                .override(60,60)
                .centerCrop()
                .into(viewHolde.imageView);

        return convertView;
    }

    // 用来保存item项视图上的子控件
    private class ViewHolde {
        TextView textView;
        ImageView imageView;

        public ViewHolde(View view) {
            textView = view.findViewById(R.id.textview);
            imageView = view.findViewById(R.id.imageview);
        }
    }
}